package com.itheima.web.controller.store;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Company;


import com.itheima.service.store.CompanyService;
import com.itheima.service.store.impl.CompanyServiceImpl;
import com.itheima.utils.BeanUtil;



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/store/company")
public class CompanyServlet extends HttpServlet {
    CompanyService companyService=new CompanyServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收operation
        String operation = request.getParameter("operation");
        //根据operation不同取值,调用不同的功能
        if(null!=operation&&operation.equals("list")){
            list(request, response);
        }else if("toAdd".equals(operation)){
            toAdd(request, response);
        }else if("save".equals(operation)){
            save(request, response);
        }else if("toEdit".equals(operation)){
            toEdit(request, response);
        }else if("update".equals(operation)){
            update(request, response);
        }else if("delete".equals(operation)){
            delete(request, response);
        }
    }


    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int page=1;
        int size=5;

        //StringUtils.isNotBlank(request.getParameter("page"))
        String pg = request.getParameter("page");
        if(pg!=null&&!pg.equals("")){
            page=Integer.parseInt(pg);
        }

        String sz = request.getParameter("size");
        if(sz!=null&&!sz.equals("")){
            size=Integer.parseInt(sz);
        }



        PageInfo pageInfo = companyService.findAll(page, size);
        //将数据存入request域对象
        request.setAttribute("page",pageInfo);
        //转发到list.jsp
        request.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(request,response);
    }
    protected void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(request,response);
    }
    protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        Company company = BeanUtil.fillBean(request, Company.class, "yyyy-MM-dd");
        System.out.println(company);
        //调用业务层接口save
        companyService.add(company);
        //跳转回到页面list
        //list(request, response);
        response.sendRedirect(request.getContextPath()+"/store/company?operation=list");
    }
    protected void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收待修改企业ID
        String id = request.getParameter("id");
        //调用业务层根据id查询企业对象
        Company company = companyService.findById(id);
        //将返回的企业对象存入域对象
        request.setAttribute("company",company);
        //转发到修改页面
        request.getRequestDispatcher("/WEB-INF/pages/store/company/update.jsp").forward(request,response);

    }
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        Company company = BeanUtil.fillBean(request, Company.class, "yyyy-MM-dd");
        System.out.println(company);
        //调用业务层接口save
        companyService.update(company);
        //跳转回到页面list
        //list(request, response);
        response.sendRedirect(request.getContextPath()+"/store/company?operation=list");
    }
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        Company company = BeanUtil.fillBean(request, Company.class, "yyyy-MM-dd");
        //调用业务层接口delete
        companyService.delete(company);
        //分页查询
        //list(request, response);
        response.sendRedirect(request.getContextPath()+"/store/company?operation=list");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
